package com.aaa.controller;

import com.aaa.entity.*;
import com.aaa.service.RoleService;
import com.github.pagehelper.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


//用户角色controller
@Controller
@RequestMapping("role")
public class RoleController extends BaseController{
    /**
     * 服务对象
     */
    @Resource
    private RoleService roleService;

    private  MyReturn myReturn = new MyReturn();

    /**
     * 页面跳转
     *
     * @return ModelAndView
     * @TeacherChen
     * @createTime 2020/12/29 23:01
     */
    @RequestMapping("toShowRole.do")
    @ResponseBody
    public ModelAndView toRole() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("showRole");
        return modelAndView;
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */

    @RequestMapping("selectOne.do")
    public Role selectOne(Integer id) {
        return this.roleService.queryById(id);
    }

    /**
     * @param pageSort:
     * @return com.sd.entity.ReturnBean
     * @create by: IvanZ
     * @description : 查询所有并分页
     * @create time: 2020/12/19 18:11
     */
    @ResponseBody
    @RequestMapping("findAll.do")
    public MyReturn findAll(PageSort pageSort) {
        Page page = roleService.findAllRole(pageSort);
        //获取分页之后的数据
        myReturn.setData(page.getResult());
        //获取总条数
        myReturn.setCount(page.getTotal());
        return getSuccessResult(myReturn);
    }

    /**
     * @param roleId:
     * @return com.sd.entity.ReturnBean
     * @create by: IvanZ
     * @description : 通过id删除数据
     * @create time: 2020/12/19 19:10
     */
    @ResponseBody
    @RequestMapping("deleteById.do")
    public MyReturn deleteById(Integer roleId) {
        boolean b = roleService.deleteById(roleId);
        if (b) {

            return getSuccessResult(myReturn);
        }
        return getFailResult(myReturn);
    }

    /**
     * @return com.sd.entity.ReturnBean
     * @create by: IvanZ
     * @description : 往role和role_menu表中添加数据
     * @create time: 2020/12/20 17:11
     */
    @ResponseBody
    @RequestMapping("insertRole.do")
    public MyReturn insertRole(Role role, String menus) {
        Role insert = roleService.insert(role);
        if (insert != null) {
            String[] menusId = menus.split(",");
            roleService.insertRoleMenu(insert.getRoleId(), menusId);
            return getSuccessResult(null);
        }
        return getFailResult(null);
    }

    /**
     * @param role:
     * @param menus:
     * @return com.sd.entity.ReturnBean
     * @create by: IvanZ
     * @description : 往role和role_menu表中修改数据
     * @create time: 2020/12/20 17:11
     */
    @ResponseBody
    @RequestMapping("updateRole.do")
    public MyReturn updateRole(Role role, String menus) {
        Role update = roleService.update(role);
        if (update != null) {
            String[] menusId = menus.split(",");
            roleService.updateRoleMenu(update.getRoleId(), menusId);
            return getSuccessResult(null);
        }
        return getFailResult(null);
    }

    /**
     * 获取树形菜单数据
     */
    @ResponseBody
    @RequestMapping("findAllMenu.do")
    public List<LayUiTree> findAllMenu(){
        List<Menu> menus=roleService.findAllMenu();

        List<LayUiTree> list=new ArrayList<>();
        for(Menu menu:menus){
            if (menu.getParentId()==0){
              LayUiTree tree=new LayUiTree();
                tree.setId(menu.getMenuId());
                tree.setTitle(menu.getMenuName());
                tree.setUrl(menu.getUrl());
//                tree.setChildren(getChildren(menu.getMenuId()),menus);
                tree.setChildren(getChildren(menu.getMenuId(), menus));
                tree.setChildren(getChildren(menu.getMenuId(), menus));
                list.add(tree);
            }
        }
        return list;
    }
    @ResponseBody
    @RequestMapping("findSelectMenu.do")
    public List<Integer> findSelectMenu(int roleId){

        return roleService.findSelectMenu(roleId);
    }

    /**
     * 递归得到孩子节点
     */

    public List<LayUiTree> getChildren(Integer parentId,List<Menu> menus){
        List<LayUiTree> list=new ArrayList<>();
        for (Menu menu : menus) {
            if (menu.getParentId().equals(parentId)){
                LayUiTree tree=new LayUiTree();
                tree.setId(menu.getMenuId());
                tree.setTitle(menu.getMenuName());
                tree.setUrl(menu.getUrl());

                tree.setChildren(getChildren(menu.getMenuId(),menus));
                list.add(tree);
            }


        }
        return list;
    }
}












